/**************************************************************************************
* @file   : CAN_Motor_TX_Task.c
* @version: 
* @date   : 2024-09-04
* @file   : George
* @brief  : 
**************************************************************************************/
#include "CAN_Motor_TX_Task.h"
#include "cmsis_os.h"
#include "FreeRTOS.h"
#include "task.h"
#include "usart.h"
#include "can_bsp.h"
#include "safe_task.h"
#include "start_task.h"
#include "custom_lib.h"
#include "relay_bsp.h"
#include "bsp_mx_driver.h"
#include "bsp_12v_driver.h"


__IO uint32_t flag_motor_err = 0;

extern osEventFlagsId_t Safe_EventHandle;
extern float motor_torque[4];

//test CAN帧
uint8_t test1[8] = {0x00, 0x1A, 0x10, 0x00, 0x00, 0x0A, 0x03, 0x03};
uint8_t test2[8] = {0x00, 0x1A, 0x10, 0x00, 0x00, 0x0A, 0x03, 0x03};

extern uint8_t pos_mode_frame[8];
extern uint8_t zero_pos_frame[8];
extern uint8_t zero_frame[8];

/**************************************************************************************
** @name:              CAN_Motor_TX_APP
** @brief:             
** @param:             
** @retval:            
** @author:            George
** @date:              2024-09-04
** @version:           V0.0
**************************************************************************************/
void CAN_Motor_TX_APP(void const * argument)
{
    /* USER CODE BEGIN KeyTask_Entry */
    /* Infinite loop */
    uint32_t tick;
    tick = osKernelGetTickCount();
    for(;;)
    {
        flag_motor_err = osEventFlagsGet(Safe_EventHandle);
//        //48V转向电机使能
//        fdcanx_send_exid_data(&hfdcan1, 0x300FD01, zero_frame, 8);
//        fdcanx_send_exid_data(&hfdcan1, 0x300FD02, zero_frame, 8);
//        //设置位置模式
//        fdcanx_send_exid_data(&hfdcan1, 0x1200FD01, pos_mode_frame, 8);
//        fdcanx_send_exid_data(&hfdcan1, 0x1200FD02, pos_mode_frame, 8);
        //        LOG_INFO(0, "\n Control_APP \r\n");
        // fdcanx_send_data(&hfdcan1, 0x666, test1, 8);
        if (0 == remoter.key.d || 0 == remoter.online)//急停，打开制动器
        {
            //发送协议电机失能以及扭矩为0
            fdcanx_send_data(&hfdcan1, 0x101, front_motor1_frame, 8);
            fdcanx_send_data(&hfdcan1, 0x101, front_motor2_frame, 8);
            fdcanx_send_data(&hfdcan1, 0x102, rear_motor1_frame, 8);
            fdcanx_send_data(&hfdcan1, 0x102, rear_motor2_frame, 8);
            fdcanx_send_data(&hfdcan1, 0x324, front_12v_motor_frame, 8);
        }
        else if (1 == remoter.key.b && 1 == remoter.key.d && 1 == remoter.online)
        {
            //原地旋转模式
            //发送协议加减速帧
        }
        else if (1 == remoter.key.c)//暂停模式
        {
            
        }
        else if (1 == remoter.key.d && 0 == remoter.key.b && 1 == remoter.online)
        {
            //发送协议电机使能以及扭矩为5A       
            fdcanx_send_data(&hfdcan1, 0x101, front_motor1_frame, 8);
            fdcanx_send_data(&hfdcan1, 0x101, front_motor2_frame, 8);
            fdcanx_send_data(&hfdcan1, 0x102, rear_motor1_frame, 8);
            fdcanx_send_data(&hfdcan1, 0x102, rear_motor2_frame, 8);
            fdcanx_send_data(&hfdcan1, 0x324, front_12v_motor_frame, 8);
        }

        tick += 20U;                      // delay 2000 ticks periodically
        osDelayUntil(tick);
    }
    /* USER CODE END KeyTask_Entry */
}

